# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
import asyncio
import re
import textwrap
from copy import deepcopy
from typing import Dict, List, Optional
import math
import json
import torch
from json import JSONDecodeError
from swift.llm import PtEngine, RequestConfig, Template, to_device
from swift.llm.infer.protocol import ChatCompletionResponse
from swift.plugin import ORM, orms
from swift.plugin.rm_plugin import DefaultRMPlugin
from swift.utils import get_logger
import torch

from collections import Counter

import re
import string
import random

def normalize_answer(s):
    def remove_articles(text):
        return re.sub(r"\b(a|an|the)\b", " ", text)

    def white_space_fix(text):
        return " ".join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return "".join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))


def em_check(prediction, golden_answers):
    if isinstance(golden_answers, str):
        golden_answers = [golden_answers]
    normalized_prediction = normalize_answer(prediction)
    score = 0
    for golden_answer in golden_answers:
        golden_answer = normalize_answer(golden_answer)
        if golden_answer == normalized_prediction:
            score = 1
            break
    return score


def subem_check(prediction, golden_answers):
    if isinstance(golden_answers, str):
        golden_answers = [golden_answers]
    normalized_prediction = normalize_answer(prediction)
    score = 0
    for golden_answer in golden_answers:
        golden_answer = normalize_answer(golden_answer)
        if golden_answer in normalized_prediction:
            score = 1
            break
    return score


def extract_solution(solution_str):
    """Extract the equation from the solution string."""
    # Remove everything before the first "Assistant:"
    # if "Assistant:" in solution_str:
    #     solution_str = solution_str.split("Assistant:", 1)[1]
    # elif "<|im_start|>assistant" in solution_str:
    #     solution_str = solution_str.split("<|im_start|>assistant", 1)[1]
    # else:
    #     return None
    # solution_str = solution_str.split('\n')[-1]

    answer_pattern = r'<answer>(.*?)</answer>'
    match = re.finditer(answer_pattern, solution_str, re.DOTALL)
    matches = list(match)
    
    if len(matches) < 1:
        return None
    
    # If there are 2 or more matches, return the last one
    return matches[-1].group(1).strip()


def compute_score_em(solution_str, ground_truth, method='strict', format_score=0., score=1.):
    """The scoring function for exact match (EM).

    Args:
        solution_str: the solution text
        ground_truth: the ground truth
        method: the method to extract the solution, choices are 'strict' and 'flexible'
        format_score: the score for the format
        score: the score for the correct answer
    """
    answer = extract_solution(solution_str=solution_str)
    do_print = random.randint(1, 64) == 1
    
    if do_print:
        print(f"--------------------------------")
        print(f"Golden answers: {ground_truth['target']}")
        print(f"Extracted answer: {answer}")
        print(f"Solution string: {solution_str}")
    
    if answer is None:
        return -1
    else:
        if em_check(answer, ground_truth['target']):
            return score
        else:
            return format_score


def compute_score_subem(solution_str, ground_truth, method='strict', format_score=0., score=1.):
    """The scoring function for substring exact match (EM).

    Args:
        solution_str: the solution text
        ground_truth: the ground truth
        method: the method to extract the solution, choices are 'strict' and 'flexible'
        format_score: the score for the format
        score: the score for the correct answer
    """
    answer = extract_solution(solution_str=solution_str)
    #do_print = random.randint(1, 64) == 1
    do_print = 1
    
    if do_print:
        print(f"-------------------------------- \n \
                Golden answers: {ground_truth['target']} \n \
                Extracted answer: {answer} \n \
                Solution string: {solution_str} \n \
                -------------------------------- \
            ")
    
    if answer is None:
        return -1
    else:
        if subem_check(answer, ground_truth['target']):
            return score
        else:
            return format_score


# infos: [{'rollout_reward': [0.1], 'num_turns': 2}, {'rollout_reward': [0.1], 'num_turns': 2}]
class R1Reward(ORM):
    def __call__(self, completions, **kwargs) -> List[float]:
        rewards = []
        n = len(completions) # batch number

        answer = kwargs.get('golden_answers')
        for comp, answ in zip(completions, answer):
           reward = compute_score_em(comp, { 'target': answ }, method='strict', format_score=0., score=1.)
           rewards.append(reward)

        return rewards
    
class R1RewardSub(ORM):
    def __call__(self, completions, **kwargs) -> List[float]:
        rewards = []
        n = len(completions) # batch number

        answer = kwargs.get('golden_answers')
        for comp, answ in zip(completions, answer):
           reward = compute_score_subem(comp, { 'target': answ }, method='strict', format_score=0., score=1.)
           rewards.append(reward)

        return rewards

orms['r1_reward'] = R1Reward
orms['r1_reward_sub'] = R1RewardSub

